<#assign baseURL = springMacroRequestContext.getContextPath() />
<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>拓深科技</title>
<#include "./css.ftl">
</head>
<body>
<div id="wrapper">

<#include "./left_nav.ftl">

    <div id="page-wrapper" class="gray-bg dashbard-1 animated fadeInRight">
    <#include "./top_nav.ftl">
        <div class="wrapper wrapper-content">

            <div class="row">
                <#if (role_type_id > 2)>

                    <div class="widget red-bg p-lg text-center">
                        <div class="m-b-lg">
                            <i class="fa fa-bell fa-4x" style="margin-bottom: 20px"></i>
                            <h1 class="font-bold no-margins">
                                权限不足
                            </h1>
                        </div>
                    </div>

                <#else >
                    <div class="col-md-3">
                        <div class="panel panel-default" >
                            <div class="panel-heading">
                                <h4 class="panel-title"><b>传感器类别列表</b></h4>
                            </div>
                            <div class="panel-body">
                                        <pre class="prettyprint" style="border: none;height: 650px;">
                                            <div id="sensor_tree"></div>
                                        </pre>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-9">
                        <div id="sensor_setting" class="panel panel-default">
                            <div class="panel-heading">
                                <h4 class="panel-title"><b>传感器类别设置</b></h4>
                            </div>
                            <div class="panel-body">
                                <div class="nav-tabs-custom">
                                    <ul class="nav nav-tabs">
                                        <li class="active"><a href="#tab_1" data-toggle="tab">录入数据</a> </li>
                                        <li><a href="#tab_2" data-toggle="tab">修改数据</a> </li>
                                        <li><a href="#tab_3" data-toggle="tab">删除数据</a> </li>
                                    </ul>
                                    <div class="tab-content">
                                        <div class="tab-pane active" id="tab_1">
                                            <div class="row">
                                                <div style="margin-left: 15px;" class="panel panel-default">
                                                    <div class="panel-header">
                                                        <h4 class="panel-title">一级目录录入</h4>
                                                    </div>
                                                    <div class="panel-body">
                                                        <div class="row">
                                                            <div class="col-sm-12">
                                                                <div class="col-sm-6">
                                                                    <div class="input-group">
                                                                        <span class="input-group-addon"><i class="glyphicon glyphicon-briefcase text-yellow"></i></span>
                                                                        <input id="level_1_input" type="text" class="form-control" placeholder="一级菜单">
                                                                    </div>
                                                                </div>
                                                                <div class="col-sm-3 col-sm-offset-3">
                                                                    <button id="levelOneSave" type="button" class="btn btn-success btn-block glyphicon glyphicon-saved pull-right"> 保存</button>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                                <div style="margin-left: 15px;" class="panel panel-default">
                                                    <div class="panel-header">
                                                        <h4 class="panel-title">二级目录录入</h4>
                                                    </div>
                                                    <div class="panel-body">
                                                        <div class="row">
                                                            <div class="col-sm-12" id="level2_begin_input">
                                                                <div class="col-sm-3">
                                                                    <div class="form-group">
                                                                        <label>一级项目选择</label>
                                                                        <select id="tab_1_level_2_select_1" class="form-control select2 tab_1_2_level_1_select" style="width: 100%"></select>
                                                                    </div>
                                                                </div>
                                                                <div class="col-sm-3">
                                                                    <div class="form-group">
                                                                        <label>二级项目录入</label>
                                                                        <div class="input-group">
                                                                            <span class="input-group-addon"><i class="glyphicon glyphicon-folder-open text-yellow"></i></span>
                                                                            <input id="tab_1_level_2_input" type="text" class="form-control">
                                                                        </div>
                                                                    </div>
                                                                </div>
                                                                <div class="col-sm-3 col-sm-offset-3">
                                                                    <button id="saveBtnTabInputLevel2" type="button" class="btn btn-success btn-block glyphicon glyphicon-saved"
                                                                            style="margin-top: 24px"> 保存</button>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                                <div style="margin-left: 15px;" class="panel panel-default">
                                                    <div class="panel-header">
                                                        <h4 class="panel-title">三级目录录入</h4>
                                                    </div>
                                                    <div class="panel-body">
                                                        <div class="row">
                                                            <div class="col-sm-12" id="level3_begin_input">
                                                                <div class="col-sm-3">
                                                                    <div class="form-group">
                                                                        <label>一级项目选择</label>
                                                                        <select id="tab_1_level_3_select_1" class="form-control select2 tab_1_3_level_1_select" style="width: 100%"></select>
                                                                    </div>
                                                                </div>
                                                                <div class="col-sm-3">
                                                                    <div class="form-group">
                                                                        <label>二级项目选择</label>
                                                                        <select id="tab_1_level_3_select_2" class="form-control select2 tab_1_3_level_2_select" style="width: 100%"></select>
                                                                    </div>
                                                                </div>
                                                                <div class="col-sm-3">
                                                                    <div class="form-group">
                                                                        <label>三级项目录入</label>
                                                                        <div class="input-group">
                                                                            <span class="input-group-addon"><i class="glyphicon glyphicon-file text-yellow"></i></span>
                                                                            <input id="tab_1_level_3_input" type="text" class="form-control">
                                                                        </div>
                                                                    </div>
                                                                </div>
                                                                <div class="col-sm-3">
                                                                    <button id="saveBtnTabInputLevel3" type="button" class="btn btn-success btn-block glyphicon glyphicon-saved"
                                                                            style="margin-top: 24px"> 保存</button>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="tab-pane" id="tab_2">
                                            <div class="row">
                                                <div class="pad margin no-print">
                                                    <div class="callout callout-warning" style="margin-bottom: 0!important;">
                                                        <h4 style="margin-left: 15px;">单击左侧树状表中的内容，然后修改文本框中的内容，最后提交。</h4>
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="row">
                                                <div class="col-sm-12">
                                                    <div style="display: none;" class="col-sm-2">
                                                        <div class="input-group">
                                                            <span class="input-group-addon"><i class="fa fa-key text-yellow"></i></span>
                                                            <input id="tree_click_id" type="text" class="form-control" disabled>
                                                        </div>
                                                    </div>
                                                    <div class="col-sm-4">
                                                        <div class="input-group">
                                                            <span class="input-group-addon"><i class="fa  fa-file-text text-yellow"></i></span>
                                                            <input id="tree_click_text" type="text" class="form-control" >
                                                        </div>
                                                    </div>
                                                    <div class="col-sm-3 col-sm-offset-3">
                                                        <button id="saveChangeBtn" type="button" class="btn btn-success glyphicon glyphicon-saved btn-block"> 保存</button>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="tab-pane" id="tab_3">
                                            <div class="row">
                                                <div class="pad margin no-print">
                                                    <div class="callout callout-danger" style="margin-bottom: 0!important;">
                                                        <h4 style="margin-left: 15px;"> 单击左侧树状表中的内容，然后执行删除操作。此过程不可逆！</h4>
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="row">
                                                <div class="col-sm-12">
                                                    <div style="display: none;" class="col-sm-2">
                                                        <div class="input-group">
                                                            <span class="input-group-addon"><i class="fa fa-key text-red"></i></span>
                                                            <input id="tree_click_id_del" type="text" class="form-control" disabled>
                                                        </div>
                                                    </div>
                                                    <div class="col-sm-4">
                                                        <div class="input-group">
                                                            <span class="input-group-addon"><i class="fa  fa-file-text text-red"></i></span>
                                                            <input id="tree_click_text_del" type="text" class="form-control" disabled>
                                                        </div>
                                                    </div>
                                                    <div class="col-sm-3 col-sm-offset-3">
                                                        <button id="delDataBtn" type="button" class="btn btn-danger glyphicon glyphicon-remove btn-block"> 删除</button>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </#if>
            </div>

        </div>
    <#include "./bottom_nav.ftl">
    </div>

</div>


<#include "./js.ftl">



<#if (role_type_id > 2)>

<#else >

<script>
    var server = "http://"+getURL()+"${baseURL}";
    var treeData = [];
    $(function () {
        socketInit(server.substring(5));
        clear();
        getTree();
    });
    function getTree() {
        $.ajax({
            url: "${baseURL}/xkSensor/getTree",
            async:false,
            type:"GET",
            success:function (data_in) {
                var jsonObj = data_in;
                treeData = [];
                for(var i in jsonObj.data){
                    var level1Data = jsonObj.data;
                    if(i == 0){
                        var level1Obj = {
                            id: [level1Data[i].id, level1Data[i].parentId],
                            text: level1Data[i].name,
                            icon: "glyphicon glyphicon-briefcase text-yellow",
                            state: {"opened" : true }
                        };
                    }else{
                        var level1Obj = {
                            id: [level1Data[i].id, level1Data[i].parentId],
                            text: level1Data[i].name,
                            icon: "glyphicon glyphicon-briefcase text-yellow",
                            state: {"opened" : false }
                        };
                    }
                    if(level1Data[i].children != undefined){
                        var level2Data = level1Data[i].children;
                        var level2Arr = [];
                        for(var j in level2Data){
                            if(i == 0 && j == 0){
                                var level2Obj = {
                                    text: level2Data[j].name,
                                    id: [level2Data[j].id,level2Data[j].parentId],
                                    icon : "glyphicon glyphicon-folder-open text-yellow",
                                    state: {"opened" : true }
                                };
                            }else{
                                var level2Obj = {
                                    text: level2Data[j].name,
                                    id: [level2Data[j].id,level2Data[j].parentId],
                                    icon : "glyphicon glyphicon-folder-open text-yellow",
                                    state: {"opened" : false }
                                };
                            }

                            if(level2Data[j].children != undefined)
                            {
                                var level3Data = level2Data[j].children;
                                var level3Arr = [];
                                for(var k in level3Data){
                                    var level3Obj = {
                                        text: level3Data[k].name,
                                        id: [level3Data[k].id,level3Data[k].parentId],
                                        icon : "glyphicon glyphicon-file text-yellow",
                                    };
                                    level3Arr.push(level3Obj);
                                }
                                level2Obj.children = level3Arr;
                            }
                            level2Arr.push(level2Obj);
                        }
                        level1Obj.children = level2Arr;
                    }
                    treeData.push(level1Obj);
                }
                $('#sensor_tree').data('jstree',false).empty().jstree({
                    'plugins':["massload"],
                    'core' : {
                        'data' : treeData
                    },

                });
            }
        });
        $('#level3_begin_input').cxSelect({
            selects: ['tab_1_3_level_1_select', 'tab_1_3_level_2_select'],
            jsonName: 'text',
            jsonValue: 'id',
            jsonSub: 'children',
            data:treeData
        });
        $('#level2_begin_input').cxSelect({
            selects: ['tab_1_2_level_1_select'],
            jsonName: 'text',
            jsonValue: 'id',
            data:treeData
        });

        $('#sensor_tree').on("select_node.jstree",function (e, data) {
            var ID = data.node.id.split(',')[0];
            var text = data.node.text;
            $('#tree_click_id').val(ID);
            $('#tree_click_text').val(text);
            $('#tree_click_id_del').val(ID);
            $('#tree_click_text_del').val(text);
        });
    };
    function clear() {
        $('#sensor_tree').empty();
    };

    $('#saveBtnTabInputLevel3').click(function () {
        if($('#tab_1_level_3_select_1').get(0).selectedIndex != 0){
            if($('#tab_1_level_3_select_2').val() != null && $('#tab_1_level_3_select_2').get(0).selectedIndex != 0){
                var level2 = $('#tab_1_level_3_select_2').val();
                if($('#tab_1_level_3_input').val()[0] != null){
                    var name = $('#tab_1_level_3_input').val();
                    if(isSpecial(name)){
                        layer.alert('名称不能含有特殊字符',{
                            closeBtn: 0
                        });
                    }else{
                        var parentId = level2.split(',')[0];
                        var formDatas = new FormData();
                        formDatas.append("name",name);
                        formDatas.append("parent_id",parentId);
                        formDatas.append("level",3);
                        $.ajax({
                            type:"post",
                            contentType : false,
                            processData:false,
                            url:'${baseURL}/xkSensor/add',
                            async:false,
                            data:formDatas,
                            success:function (data) {
                                if(data.code == 0){
                                    $('#tab_1_level_3_input').val("");
                                    clear();
                                    getTree();
                                    layer.alert('数据录入成功！',{
                                        closeBtn: 0
                                    });
                                }
                                else{
                                    layer.alert('操作失败：' + data,{
                                        closeBtn: 0
                                    });
                                }
                            }
                        });
                    }

                }
            }
        }
    });

    $('#saveBtnTabInputLevel2').click(function () {
        if($('#tab_1_level_2_select_1').get(0).selectedIndex != 0){
            if($('#tab_1_level_2_input').val()[0] != null){
                var name = $('#tab_1_level_2_input').val();
                if(isSpecial(name)){
                    layer.alert('名称不能含有特殊字符',{
                        closeBtn: 0
                    });
                }else{
                    var level1 = $('#tab_1_level_2_select_1').val();
                    var parentId = level1.split(',')[0];
                    var formDatas = new FormData();
                    formDatas.append("name",name);
                    formDatas.append("parent_id",parentId);
                    formDatas.append("level",2);
                    $.ajax({
                        type:"post",
                        contentType : false,
                        processData:false,
                        url:'${baseURL}/xkSensor/add',
                        async:false,
                        data:formDatas,
                        success:function (data) {
                            if(data.code == 0){
                                $('#tab_1_level_2_input').val("");
                                clear();
                                getTree();
                                layer.alert('数据录入成功！',{
                                    closeBtn: 0
                                });
                            }
                            else{
                                layer.alert('操作失败：' + data,{
                                    closeBtn: 0
                                });
                            }
                        }
                    });
                }

            }
        }
    });

    $('#levelOneSave').click(function () {
        var name = $('#level_1_input').val();
        if(isSpecial(name)){
            layer.alert('名称不能含有特殊字符',{
                closeBtn: 0
            });
        }else{
            var formDatas = new FormData();
            formDatas.append("name",name);
            formDatas.append("parent_id",0);
            formDatas.append("level",1);
            if(name != ""){
                $.ajax({
                    type:"post",
                    contentType : false,
                    processData:false,
                    url: "${baseURL}/xkSensor/add",
                    data:formDatas,
                    success:function (data) {
                        if(data.code == 0){
                            $('#level_1_input').val("");
                            clear();
                            getTree();
                            layer.alert('数据录入成功！',{
                                closeBtn: 0
                            });
                        }
                        else{
                            layer.alert('操作失败：' + data,{
                                closeBtn: 0
                            });
                        }
                    }
                });
            }else{
                layer.alert('请输入数据！',{
                    closeBtn: 0
                });
            }
        }
    });
    $('#saveChangeBtn').click(function () {
        var name = $('#tree_click_text').val();
        if(isSpecial(name)){
            layer.alert('名称不能含有特殊字符',{
                closeBtn: 0
            });
        }else{
            var ID = $('#tree_click_id').val();
            var formDatas = new FormData();
            formDatas.append("id",ID);
            formDatas.append("name",name);
            if(ID && name){
                $.ajax({
                    type:"post",
                    contentType : false,
                    processData:false,
                    url: '${baseURL}/xkSensor/updateName',
                    data:formDatas,
                    success:function (data) {
                        if(data.code == 0){
                            clear();
                            getTree();
                            layer.alert('修改成功！',{
                                closeBtn: 0
                            });
                        }
                        else{
                            layer.alert('操作失败：' + data,{
                                closeBtn: 0
                            });
                        }
                    }
                });
            }else{
                layer.alert('请先点选树状图内的目标和输入要修改的内容。',{
                    closeBtn: 0
                });
            }
        }


    });

    $('#delDataBtn').click(function () {
        var ID = $('#tree_click_id_del').val();
        if(ID != null){
            $.ajax({
                url: '${baseURL}/xkSensor/delete?id='+ ID,
                success:function (data) {
                    if(data.code == 0){
                        $('#tree_click_id_del').val("");
                        $('#tree_click_text_del').val("");
                        clear();
                        getTree();
                        layer.alert('删除成功！',{
                            closeBtn: 0
                        });
                    }
                    else{
                        layer.alert('操作失败：' + data,{
                            closeBtn: 0
                        });
                    }
                }
            });
        }
        else{
            layer.alert('请先点选树状图内的目标。',{
                closeBtn: 0
            });
        }
    });
    function isSpecial(str){
        var pattern = new RegExp("[`~!@#$^&*()=|{}':;',\\[\\].<>/?~！@#￥……&*（）&mdash;—|{}【】‘；：”“'。，、？]");
        if(pattern.test(str)){
            return 1;
        }else {
            return 0;
        }
    }
</script>

</#if>

</body>
</html>